<template>
  <PageDialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <PageFilter
      v-model:query="dialogInfoFilterInfo.query"
      :filter-list="dialogInfoFilterInfo.list"
    />
    <div class="flex_full_item">
      <!-- 表格 -->
      <PageTable
        :refresh="dialogTableInfo.refresh"
        :init-curpage="dialogTableInfo.initCurpage"
        v-model:data="dialogTableInfo.data"
        :api="dialogTableInfo.api"
        v-model:query="dialogInfoFilterInfo.query"
        :field-list="dialogTableInfo.fieldList"
        :highlight-row="true"
        :check-box="false"
        :isDynamic="true"
        @handleEvent="handleEvent"
      ></PageTable>
    </div>
  </PageDialog>
</template>

<script setup name="selectOrder">
import { getUseExportPage, getScantoPurchReturn } from '@/api/use.js'
const { proxy } = getCurrentInstance()
const props = defineProps({
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: () => {
      return {}
    },
  },
})

const dialogInfoFilterInfo = ref({
  query: {},
  list: [
    {
      type: 'input',
      label: '单号',
      value: 'orderNoLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
const dialogInfo = ref({
  width: '60%',
  visible: false,
  title: '选择领用退回入库单',
  btLoading: false,
  btList: [
    { label: '关闭', event: closeDialog, show: true },
    {
      label: '保存',
      type: 'primary',
      event: selectOrder,
      saveLoading: false,
      show: true,
    },
  ],
})
const dialogTableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getUseExportPage,
  selections: {},
  fieldList: [
    { label: '入库单号', value: 'orderNo' },
    { label: '退回人', value: 'returnUserName' },
    { label: '入库时间', value: 'updateTime' },
  ],
})

const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val) // 更新父组件shows
  },
})

function getList() {
  dialogTableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  const dialogTableInfo = dialogTableInfo.value
  switch (event) {
    case 'tableClickRow':
      dialogTableInfo.selections = data
      break
  }
}
//选择产品
function selectOrder() {
  if (dialogTableInfo.value.selections && dialogTableInfo.value.selections.id) {
    let params = {
      useExportId: dialogTableInfo.value.selections.id,
      sort: 'product_id desc',
    }
    getScantoPurchReturn(params).then((res) => {
      if (res && res.success) {
        proxy.$emit('callback', res.data)
        closeDialog()
      }
    })
  } else {
    proxy.$message({
      message: '请选择领用退回单',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
  }
}
function closeDialog() {
  shows.value = false
  proxy.$emit('reset')
}
function search() {
  // 重置分页
  dialogTableInfo.value.initCurpage = Math.random()
  dialogTableInfo.value.refresh = Math.random()
}
onMounted(() => {
  proxy.$nextTick(() => {
    dialogInfoFilterInfo.value.query = {
      ...props.query,
    }
    getList()
  })
})
</script>

<style scoped></style>
